package com.gxuwz.medical.domain.institution;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.exception.DbException;

/**
 * 农合机构管理类
 * @author 演示
 *
 */
public class Institution {
	
	/**
	 * 行政区域编码，类标识符
	 */
	private Area area;
	/**
	 * 经办机构编码
	 */
	private String agencode;
	
	/**
	 * 经办机构名称
	 */
	private String agenName;
	/**
	 * 经办机构级别，区别于行政区域级别，1表示县级农合办，2表示镇级农合点
	 */
	private int grade;
	
	private String name;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Institution(){
		
	}
	/**
	 * 实例化对象
	 * @param areacode
	 * @throws Exception 
	 */
	public Institution(Area area)throws Exception{
		  if(area==null){
			  throw new IllegalStateException("行政区域编码不能空");
		  }
		try{
			this.area=area;
			loadDB();
	    }catch(SQLException e){
	    	throw new DbException("加载id="+this.area.getAreacode()+"农合经办机构记录失败", e);
	    }
	}
	
	public Institution(Area area, String agencode, String agenName, int grade,String name) {
		super();
		this.area = area;
		this.agencode = agencode;
		this.agenName = agenName;
		this.grade = grade;
		this.name = name;
	}
	/**
	 * 
	 * @param areaid
	 * @throws Exception 
	 */
	private void loadDB()throws Exception{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DBUtil.getConn();
			pstmt=conn.prepareStatement("select * from institution where areacode=?");
			int index=1;
			pstmt.setString(index, this.area.getAreacode());
			rs=pstmt.executeQuery();
			if(rs.next()){
				this.area=new Area(rs.getString("areacode"));
				this.agencode=rs.getString("agencode");
				this.agenName=rs.getString("agenName");
				this.grade=rs.getByte("grade");
				this.name = rs.getString("name");
			}
		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(rs, pstmt, conn);
		}
	}
	/**
	 * 添加农合经办点
	 * @param areacode
	 * @param agencode
	 * @param agenname
	 * @param grade
	 * 
	 */
	public void addInst(Area area,String agencode,String agenName,int grade,String name)throws Exception{
		Connection conn=null;
		try{
			//初始化属性
			System.out.println("(Area area======"+area);
			this.area=area;
			this.agencode=agencode;
			this.agenName=agenName;
			this.grade =grade;
			this.name =name;
		   //保存导数据库
			conn=DBUtil.getConn();
			conn.setAutoCommit(false);
			//调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		}catch(Exception e){
			conn.rollback();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
		
	}
	/**
	 * 插入记录
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection)throws SQLException{
		PreparedStatement pstmt=null;
		try{

			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into institution(areacode,agencode,agenName,grade,name)");
			sqlBuff.append("values(?,?,?,?,?)");
			String sql=sqlBuff.toString();
			pstmt=connection.prepareStatement(sql);
			//依次设置动态参数的值，注意顺序
			int index=1;
			pstmt.setString(index++, this.area.getAreacode());//行政区域编码
			pstmt.setString(index++, this.agencode);
			pstmt.setString(index++, this.agenName);
			pstmt.setInt(index, this.grade);
			pstmt.setString(index++, this.name);
			//以executeUpdate()执行SQL
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw e;
		}finally{
			//只关闭PrepareStatement资源
			if(pstmt!=null){
				pstmt.close();
			}
			
		}
		
	}
	/**
	 * 修改行政区域
	 * @param area
	 * @param agencode
	 * @param agenname
	 * @param grade
	 * @throws Exception
	 */
	public void editInst(Area area,String agencode,String agenName,int grade,String name)throws Exception{
		Connection conn=null;
		try{
			//初始化属性
			System.out.println("(Area area======"+area);
			this.area=area;
			this.agencode=agencode;
			this.agenName=agenName;
			this.grade =grade;
			this.name =name;
		   //保存导数据库
			conn=DBUtil.getConn();
			conn.setAutoCommit(false);
			//调用插入数据库私有方法
			this.editToDB(conn);
			conn.commit();
		}catch(Exception e){
			conn.rollback();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
		
	}
	
	/**
	 * 将修改数据存到数据库
	 * @param conn
	 * @throws SQLException
	 */
	private void editToDB(Connection conn)throws SQLException{
		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("update institution t set t.agencode=?,agenName=?,grade=?,name=?  where t.areacode=?");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.agencode);
			  pstmt.setString(index++, this.agenName);
			  pstmt.setInt(index++, this.grade);
			  pstmt.setString(index++, name);
			  pstmt.setString(index, this.area.getAreacode());
			  System.out.println("this.area.getAreacode()============="+this.area.getAreacode());
			  pstmt.executeUpdate();
		  }catch(SQLException e){
			  throw new SQLException("Failed to update institution  !", e);
		  }finally{
			  DBUtil.close(pstmt);
		  }
		}
	
	public void delInst(Area area)throws SQLException{

		Connection conn=null;
       try{
    	   //1:对象属性赋值
    	   this.area=area;
		   conn=DBUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			   String areacode =this.area.getAreacode();
		    	 
			//定义构造插入SQL语句字符串变量sql
			 String  sql="delete from institution where areacode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, areacode);
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	
	public String getAgencode() {
		return agencode;
	}
	public void setAgencode(String agencode) {
		this.agencode = agencode;
	}
	public String getAgenName() {
		return agenName;
	}
	public void setAgenName(String agenName) {
		this.agenName = agenName;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public Area getArea() {
		return area;
	}
	public void setArea(Area area) {
		this.area = area;
	}

}

